# 类的继承
# 动物类
class Animal(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight

    def run(self):
        print("animal is running")

    def speak(self):
        print("Animal")


# 狗类
class Dog(Animal):
    def __init__(self, name, weight, type):
        # 手动调用父类的初始化方法
        super(Dog, self).__init__(name, weight)
        self.type = type

    def run(self):
        print("Dog is running")


# 猫类
class Cat(Animal):
    # 不定义初始化方法，则默认调用父类的初始化方法
    def run(self):
        print("Cat is running")


class Duck(Animal):
    pass


animal = Animal("001", '20')
animal.run()
print(animal.name)

# 子类继承父类 子类可以获得父类的属性及方法
dog = Dog("002", "30", "dog")
dog.run()
cat = Cat("003", "25")
cat.run()
duck = Duck("004", "15")
duck.run()

print(isinstance(dog, Dog))
print(isinstance(cat, Cat))
print(isinstance(animal, Animal))
print(isinstance(dog, Animal))
print(isinstance(cat, Animal))
print(isinstance(animal, object))


class Stu():
    def run(self):
        print("I can running")


stu = Stu()


# 多态
# 子类继承父类
# 子类重新实现父类的方法
# 父类引用指向子类对象（在Python中不明显）
def twiceRun(animal: Animal):
    animal.run()
    animal.run()


twiceRun(animal)
twiceRun(dog)
twiceRun(cat)
twiceRun(duck)

# 因为Python是一门动态类型的语言
# 所以尽管Stu类不是Animal的子类，但还是可以调用twiceRun方法
twiceRun(stu)

list1 = [1, 2, 3]
list1[1]
list1[2]
list1[3]